package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.param.UserLocationParam;
import com.tarena.lbs.business.pojo.query.AreaStoreQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.servcie.StoreService;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.api.UserApi;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@Tag(name = "店铺相关功能接口")
@RestController
public class StoreController {
    @Autowired
    private StoreService storeService;

    @DubboReference
    private UserApi userApi;

    @Operation(summary = "查询店铺列表")
    @PostMapping("/admin/basic/store/getAll")
    public Result<PageResult<StoreVO>> pageList(@RequestBody StoreQuery query)
        throws BusinessException {
        PageResult<StoreVO> pageStoreResult = storeService.pageStoreList(query);
        return new Result(pageStoreResult);
    }

    /**
     * 新增店铺
     * @param param
     * @return
     * @throws BusinessException
     */
    @Operation(summary = "新增店铺")
    @PostMapping("/admin/basic/store/add")
    public Result<Void> save(@RequestBody StoreParam param)
        throws BusinessException {
        storeService.addStore(param);
        return Result.success();
    }

    //查询 属于当前登录用户 所属商家 和满足区域条件的店铺列表
    @Operation(summary = "查询区域下的店铺列表")
    @PostMapping("/admin/basic/store/getStoreByCity")
    public Result<PageResult<StoreVO>> getStoreByCity(@RequestBody AreaStoreQuery query)
            throws BusinessException {
        try {
            // 使用正确的工具类或上下文获取登录用户ID
            // 假设使用 AuthenticationContextUtils 获取当前用户
            UserPrinciple currentUser = AuthenticationContextUtils.get();
            if (currentUser == null) {
                throw new BusinessException("-2", "用户未登录");
            }

            Integer adminId = currentUser.getId();

            // 调用用户服务获取账号信息得到商家id
            AdminDto adminDto = userApi.getAdminById(adminId);
            if (adminDto == null) {
                throw new BusinessException("-3", "用户信息不存在");
            }

            Integer businessId = adminDto.getBusinessId();

            // 参数校验
            if (query.getCityIdList() == null || query.getCityIdList().isEmpty()) {
                throw new BusinessException("-4", "城市信息不能为空");
            }

            // 通过区域id列表和商家id查询店铺列表，进行分页查询
            PageResult<StoreVO> pageResult = storeService.queryStoresByAreaAndBusiness(
                    query.getCityIdList(),
                    businessId,
                    query.getPageNo(),
                    query.getPageSize()
            );

            // 封装结果返回
            return new Result<>(pageResult);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            log.error("查询区域店铺列表失败: ", e);
            throw new BusinessException("-5", "查询失败，请稍后重试");
        }
    }
    //手机小程序定位上报
    @Operation(summary = "手机小程序定位上报")
    @PostMapping("/basic/store/location")
    public Result<Void> location(@RequestBody UserLocationParam param)
        throws BusinessException{

        storeService.location(param);
        return Result.success();
    }

}
